package fc;

import com.alibaba.fastjson.JSON;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.upgradetool.wscheck.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;

public class TEST001 implements Action{

	@Override
	public String execute(RequestInfo info) {

		RecordSet rs = new RecordSet();
		BaseBean log = new BaseBean();

		log.writeLog("接口TEST001开始调用-----------------------------------");



		String requestid = info.getRequestid();
		String tablename = info.getRequestManager().getBillTableName();
		String nodename = "";//1
		String currentnodetype = "";//
		String status = "";//
		String sql=
		"select top 1 o3.nodename,o2.status,o2.currentnodetype,o1.requestid  from "+tablename+" o1 with(nolock) ,workflow_requestbase o2 with(nolock) ,workflow_nodebase o3 with(nolock) "+
		" where o1.requestid=o2.requestid and o2.currentnodeid=o3.id  and o1.requestid="+requestid;

		rs.executeSql(sql);
		if(rs.next()){
			nodename = Util.null2String(rs.getString("nodename"));
			currentnodetype = Util.null2String(rs.getString("currentnodetype"));
			status = Util.null2String(rs.getString("status"));
		}
		String param="requestid="+requestid+"&nodename="+nodename+"&currentnodetype="+currentnodetype+"&status="+status;
		String result = sendPost(FcHttpCon.TEST001,param);

		log.writeLog("接口TEST001返回-----------------------------------"+result);
		Object succesResponse = JSON.parse(result);
		Map map = (Map)succesResponse;

		if("ERROR".equals(map.get("msg"))){
			//记录数据
			RecordSet rs1 = new RecordSet();
			String sql1="INSERT INTO FC_API_DIAOYONG_JILU VALUES('TEST001',"+requestid+",GETDATE())";
			rs1.executeSql(sql1);
		}
		return SUCCESS;
	}

	public static String sendPost(String url, String param) {

		PrintWriter out = null;
		BufferedReader in = null;
		StringBuilder result = new StringBuilder();
		try {
			URL realUrl = new URL(url);
			// 打开和URL之间的连接
			URLConnection conn = realUrl.openConnection();
			// 设置通用的请求属性
			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("connection", "Keep-Alive");
			conn.setRequestProperty("user-agent",
					"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
			conn.setRequestProperty("Accept-Charset", "UTF-8");
			conn.setRequestProperty("contentType", "UTF-8");
			// 发送POST请求必须设置如下两行
			conn.setDoOutput(true);
			conn.setDoInput(true);
			// 获取URLConnection对象对应的输出流
			out = new PrintWriter(conn.getOutputStream());
			// 发送请求参数
			out.print(param);
			// flush输出流的缓冲
			out.flush();
			// 定义BufferedReader输入流来读取URL的响应
			in = new BufferedReader(
					new InputStreamReader(conn.getInputStream()));
			String line;
			while ((line = in.readLine()) != null) {
				result.append(line);
			}
		} catch (Exception e) {
//			System.out.println("发送 POST 请求出现异常！" + e);
			result = new StringBuilder("{\"msg\":\"SB\",\"errCode\":\"1001\"}");
			e.printStackTrace(); 
		}
		// 使用finally块来关闭输出流、输入流
		finally {
			try {
				if (out != null) {
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return result.toString();
	}




 
} 